package com.diudiutechnology.www.yadiudiu.util.http;

import android.content.Context;
import android.util.Log;

import com.diudiutechnology.www.yadiudiu.yadiudiu.singlecall.MyActivity.CheckSumBuilder;
import com.zhy.http.okhttp.OkHttpUtils;

import java.io.File;
import java.util.Calendar;
import java.util.HashMap;

/**
 * Created by Administrator on 2016/6/30.
 */
public class OkHttpClientManager {
    //    192.168.199.177:8080
    private static OkHttpClientManager mInstance;
    public static final String mBaseUrl = "http://ydd.xtangguo.com/index.php/App/";//客户
    public static final String  appKey="cfyfyp319195rc";
    public static final String  appSecret="45e99384e344e5f76fd7146a32cbb409";
    public String  curTime;
    public static final String  nonce="987654321";
    public  String  signature;
    long  MI;
    private Context context;
    HashMap<String,String> verifymap;
    private OkHttpClientManager(Context context) {
        this.context = context;
    }

    public static OkHttpClientManager getInstance(Context context) {
        if (mInstance == null) {
            synchronized (OkHttpClientManager.class) {
                if (mInstance == null) {
                    mInstance = new OkHttpClientManager(context.getApplicationContext());
                }
            }
        }
        return mInstance;
    }

    /**
     * post提交字符串
     */
    public void postUser(String string, HashMap<String, String> params, UserCallback UserCallback) {
        verify();
        String url = mBaseUrl + string;
        OkHttpUtils
                .post()
                .headers(verifymap)
                .url(url)
                .params(params)
                .build()
                .execute(UserCallback);
    }
    public void getString(String url,HashMap<String, String> params,UserCallback userCallback){
        verify();
        url=mBaseUrl+url;
        OkHttpUtils
                .get()
                .headers(verifymap)
                .url(url)
                .params(params)
                .build()
                .execute(userCallback);
    }
    /**
     * 条形码
     */
    public void verify(){
        Calendar  cld=Calendar.getInstance();
        MI=cld.get(Calendar.MILLISECOND);
        curTime=MI+"";
        signature= CheckSumBuilder.getInstance(context.getApplicationContext()).getSignature(appSecret,nonce,curTime);
        verifymap=new HashMap<String, String>();
//        verifymap.put("appKey",appKey);
//        verifymap.put("appSecret",appSecret);
        verifymap.put("curTime",MI+"");
//        verifymap.put("nonce",nonce);
        verifymap.put("signature",signature);
//        Log.e("PayActivity", "安全吗" + appKey+"  "+appSecret+"  "+MI+"  "+nonce+"  "+signature);
    }
}